
  const cloud = require('wx-server-sdk')
  cloud.init({
    env: "test4-ht5oe"
  })

  const db = cloud.database()
  const _ = db.command
  const MAX_LIMIT = 100
  var $ = db.command.aggregate
// 云函数入口函数
exports.main = async (event, context) => {
  // 先取出集合记录总数
  const countResult = await db.collection('staffAchievement').count()
  const total = countResult.total
  // 计算需分几次取
  const batchTimes = Math.ceil(total / 100)
  // 承载所有读操作的 promise 的数组
  const tasks = []
  for (var i = 0; i < batchTimes; i++) {
    // console.log("次数"+i)
    const promise = await db.collection('staffAchievement').aggregate()
    // .match({
    //   UserID: event.userid,
    //   isweihu:false
    // })
    .lookup({
      from: 'staffInformationData',
      localField: 'teachID',
      foreignField: 'TechPhone',//TechPhone
      as: 'Info',
    }).sort({
      date:1,
      teachID:1
    }).skip(i*MAX_LIMIT).limit(MAX_LIMIT)
    .replaceRoot({
      newRoot: $.mergeObjects([ $.arrayElemAt(['$Info', 0]), '$$ROOT' ])
    })
    // .match({
    //   TuinaID: _.exists(true)
    // })
    .project({
      Info:0,
      TechPicture:0,
      TechEvaluation:0
    })
    .end()
    // return promise
    
    tasks.push(promise.list)
  }
  // 等待所有
  // console.log("哈哈哈哈"+tasks)
   return tasks

}

